package com.fengxu.gamescript.jsengine.api;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.fengxu.gamescript.R;
import com.fengxu.gamescript.jsengine.annotation.JsCallJava;
import com.fengxu.gamescript.jsengine.annotation.JsObject;
import com.fengxu.gamescript.jsengine.exception.ScriptInterruptException;
import com.fengxu.gamescript.jsengine.exception.ScriptRunningException;
import com.fengxu.gamescript.service.AutoAccessibilityService;
import com.fengxu.gamescript.util.CommonUtils;
import com.fengxu.gamescript.util.PermissionUtils;
import com.fengxu.script.JsEngine;
import com.xuexiang.xui.utils.DensityUtils;
import com.yhao.floatwindow.FloatWindow;
import com.yhao.floatwindow.MoveType;
import com.yhao.floatwindow.Screen;

import java.util.HashMap;

/**
 * 全局注册入方法
 */
@JsObject(name = "__global__")
public class Global implements IJsObject {

    private Thread mThread;

    private JsEngine mEngine;

    private Context mContext;

    private Handler uiHandle;

    @Override
    public String getNameInJs() {
        return "__global__";
    }

    public Global(JsEngine jsEngine, Context context, Handler handler){
        this.mEngine = jsEngine;
        this.mContext = context;
        this.uiHandle = handler;
        mThread = Thread.currentThread();
    }

    public JsEngine getmEngine() {
        return mEngine;
    }

    public Context getmContext() {
        return mContext;
    }

    /**
     * 获取uiHandle
     * @return
     */
    @JsCallJava(statement = "获取uiHandle")
    public Handler getUiHandle() {
        return uiHandle;
    }

    /**
     * 点击屏幕指定位置
     * @param x x坐标
     * @param y y坐标
     * @param time  按下时间
     */
    @JsCallJava(statement = "点击屏幕指定位置")
    public void press(Double x, Double y, Double time){
        AutoAccessibilityService accessibilityService = getAccessibilityService();
        accessibilityService.autoClick(x,y, time.intValue());
    }

    /**
     * 将鼠标从(x1,y1)移动到(x2,y2)
     * @param time 移动时长
     */
    @JsCallJava(statement = "将鼠标从(x1,y1)移动到(x2,y2)")
    public void move(Double x1,Double y1, Double x2, Double y2, Double time){
        AutoAccessibilityService accessibilityService = getAccessibilityService();
        accessibilityService.move(x1.floatValue(),y1.floatValue(),x2.floatValue(),y2.floatValue(),time.intValue());
    }

    /**
     * 暂停当前线程指定时间
     * @param time 需要暂停的时间
     */
    @JsCallJava(statement = "暂停当前线程指定时间")
    public void sleep(Double time){
        try {
            Thread.sleep(time.longValue());
        } catch (InterruptedException e) {
            throw new ScriptInterruptException();
        }
    }

    /**
     * 弹出全局toast
     * @param text 带弹出的文字
     * @param time  弹出时长(ms)
     */
    @JsCallJava(statement = "弹出全局toast")
    public void toast(String text, Double time){
        uiHandle.post(()->{
            CommonUtils.INSTANCE.showDesktopToast(mContext,text,time.longValue());
        });
    }

    /**
     * 获取屏幕分辨率
     * @return 分辨率map
     */
    @JsCallJava(statement = "获取屏幕分辨率")
    public Object getScreenMetric(){
        DisplayMetrics screenMetric = DensityUtils.getScreenMetrics(true);
        HashMap<String,Integer> map = new HashMap();
        map.put("width", screenMetric.widthPixels);
        map.put("height",screenMetric.heightPixels);
        map.put("densityDpi",screenMetric.densityDpi);
        return map;
    }

    /**
     * 将java对象转为Json字符串
     * @param object java对象
     * @return json字符串
     */
    @JsCallJava(statement = "将java对象转为Json字符串")
    public String javaToJson(Object object){
        if(object == null) throw new NullPointerException("待转换对象不能为null");
        return JSON.toJSONString(object);
    }

    /**
     * 复制文字至系统剪贴板
     * @param text  要复制的文字
     */
    @JsCallJava(statement = "复制文字至系统剪贴板")
    public void setClipboardText(String text){
       ClipboardManager clipboardManager = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
       clipboardManager.setPrimaryClip(ClipData.newPlainText("脚本代码",text));
    }

    /**
     * 退出脚本环境
     */
    @JsCallJava(statement = "退出脚本环境")
    public void exit(){
        mEngine.exit();
    }


    /**
     * 检测无障碍是否可用，不可用则抛出异常
     */
    public static AutoAccessibilityService getAccessibilityService(){
        if(!PermissionUtils.INSTANCE.accessibilityIsEnabled()){
            throw new ScriptRunningException("无障碍服务未开启，不可运行该方法!");
        }
        return AutoAccessibilityService.Companion.getAccessibilityService();
    }


}
